﻿using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PcProject.Infrastructure.Repository
{
    public class SpecificaRepository: DapperRepositoryBase<Specification>, ISpecificaRepository
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IConfiguration _configuration;
        public SpecificaRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }


        /// <summary>
        /// 显示商品规格
        /// </summary>
        /// <returns></returns>
        public List<Specification> GetSpecifications()
        {
            string sql = $"select * from Specification";
            return (List<Specification>)Conn.Query<Specification>(sql);
        }

        /// <summary>
        /// 添加商品规格
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> AddSpecAsyn(Specification obj)
        {
            return (int)await Conn.InsertAsync(obj);
        }

        /// <summary>
        /// 删除商品规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleSpecAsync(int id)
        {
            string sql = $"delete from Specification where SpeciId={id}";
            return await Conn.ExecuteAsync(sql);
        }

        /// <summary>
        /// 反填规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Specification FillSpecAsync(int id)
        {
            return Conn.Get<Specification>(id);
        }

        /// <summary>
        /// 修改规格
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> UptSpecAsync(Specification obj)
        {
            return await Conn.UpdateAsync(obj);
        }

    }
}
